/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.ui_builders;

import br.ufes.cwtools.joinus.R;

import java.util.Calendar;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import br.ufes.cwtools.joinus.profile_manager.profile.Professional;
import br.ufes.cwtools.joinus.profile_manager.profile.Profile;


/*
 * this class does all the programatic UI work
 * it is also responsable for exporting a object containing the data
 * acquired from the UI  
*/
public class ProfessionalUI extends ProfileUI {
	boolean changed = false; // has the fields changed?
	View myView; // contains the view that that is holding the UI
	private Spinner education, degree, degreeYear, ind, subind; //to save some cpu

	@Override
	public void importPro(Profile p) {
		Professional pr = (Professional)p;

		education.setSelection(pr.education);
		degree.setSelection(pr.degree);
		
		final Calendar c = Calendar.getInstance();
		degreeYear.setSelection(c.get(Calendar.YEAR) - pr.year - 70); //  TODO
		
		ind.setSelection(pr.industry);
		subind.setSelection(pr.subInd);

		((EditText) myView.findViewById(R.id.high_school_text)).setText(pr.highSchool);
		((EditText) myView.findViewById(R.id.college_uni_text)).setText(pr.college);
		((EditText) myView.findViewById(R.id.major_text)).setText(pr.major);
		((EditText) myView.findViewById(R.id.occupation_text)).setText(pr.occupation);
		((EditText) myView.findViewById(R.id.comp_org__text)).setText(pr.company);
		((EditText) myView.findViewById(R.id.comp_wp__text)).setText(pr.companyWeb);
		((EditText) myView.findViewById(R.id.title_text)).setText(pr.title);
		((EditText) myView.findViewById(R.id.jd_text)).setText(pr.jobDesc);
		((EditText) myView.findViewById(R.id.work_email__text)).setText(pr.workemail);
		((EditText) myView.findViewById(R.id.work_phone_text)).setText(pr.workphone);
		((EditText) myView.findViewById(R.id.carrer_skills_text)).setText(pr.careerSkills);
		((EditText) myView.findViewById(R.id.carrer_int_text)).setText(pr.careerInt);
	}

	public ProfessionalUI() {
		this.layoutID = R.layout.professional;
	}

	public void SetUpSpinners(View v) {
		Spinner s = education = (Spinner) v.findViewById(R.id.education_chooser);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(v.getContext(), R.array.education_level,
				android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		degree = s = (Spinner) v.findViewById(R.id.degree_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.degree, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		final Calendar c = Calendar.getInstance();
		int currentYear = c.get(Calendar.YEAR);

		degreeYear = s = (Spinner) v.findViewById(R.id.degree_year_chooser);
		adapter = new ArrayAdapter<CharSequence>(v.getContext(),
				android.R.layout.simple_spinner_item);
		for (int i = currentYear - 70; i < currentYear + 5; i++)
			adapter.add(i + ""); //adapter.addObject(i + "");
		s.setAdapter(adapter);

		ind = s = (Spinner) v.findViewById(R.id.industry_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.industry, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		subind = s = (Spinner) v.findViewById(R.id.sub_industry_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.subactivity, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);
	}
	
	public String toString() {
		return "Professional";
	}
	
	public void setUpWidgets(View v) {
		myView=v;
		changed = true;
		SetUpSpinners(v);
	}
	
	// wraps the data acquired in the UI inside a object
	@Override
	public Profile export() {
		if (changed) {
			Professional professional = new Professional();
			professional.education = education.getSelectedItemPosition();
			professional.highSchool = ((EditText) myView.findViewById(R.id.high_school_text)).getText().toString();
			professional.college = ((EditText) myView.findViewById(R.id.college_uni_text)).getText().toString();
			professional.major = ((EditText) myView.findViewById(R.id.major_text)).getText().toString();
			professional.degree = degree.getSelectedItemPosition();
			professional.year = Integer.parseInt(degreeYear.getSelectedItem().toString());
			professional.occupation = ((EditText) myView.findViewById(R.id.occupation_text)).getText().toString();
			professional.industry = ind.getSelectedItemPosition();
			professional.subInd = subind.getSelectedItemPosition();
			professional.company =  ((EditText) myView.findViewById(R.id.comp_org__text)).getText().toString();
			professional.companyWeb = ((EditText) myView.findViewById(R.id.comp_wp__text)).getText().toString();
			professional.title = ((EditText) myView.findViewById(R.id.title_text)).getText().toString();
			professional.jobDesc = ((EditText) myView.findViewById(R.id.jd_text)).getText().toString();
			professional.workemail = ((EditText) myView.findViewById(R.id.work_email__text)).getText().toString();
			professional.workphone = ((EditText) myView.findViewById(R.id.work_phone_text)).getText().toString();
			professional.careerSkills = ((EditText) myView.findViewById(R.id.carrer_skills_text)).getText().toString();
			professional.careerInt = ((EditText) myView.findViewById(R.id.carrer_int_text)).getText().toString();
			return professional;
		}
		return null;
	}
}
